package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class activityIntent extends AppCompatActivity implements View.OnClickListener {
    private EditText et_main_number;
    private EditText et_main_sms;
    private Button btn_main_call;
    private Button btn_main_send;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent2);

        // 初始化视图对象
        et_main_number = (EditText) findViewById(R.id.et_main_number);
        et_main_sms = (EditText) findViewById(R.id.et_main_sms);
        btn_main_call = (Button) findViewById(R.id.btn_main_call);
        btn_main_send = (Button) findViewById(R.id.btn_main_send);
        // 设置监听事件
        btn_main_send.setOnClickListener(this);
        btn_main_call.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(); // 创建Intent对象
        if(view == btn_main_call){ // 打电话
            // 1.创建一个Intent（隐式）
            intent.setAction(intent.ACTION_DIAL); // 调用拨号的面板
            //2.携带数据
            String number = et_main_number.getText().toString();
            intent.setData(Uri.parse("tel:"+number));
            //3.startActivity(intent)
            startActivity(intent);

        }else if(view==btn_main_send){
            Toast.makeText(activityIntent.this, "发送短信", Toast.LENGTH_SHORT).show();

            //1.得到SmsManager的对象
            intent.setAction(intent.ACTION_SENDTO); // 调用发送短信息
            // 2.发送文本信息
            String number = et_main_number.getText().toString();
            String sms = et_main_sms.getText().toString();
            intent.setData(Uri.parse("smsto:" +number)); // 设置要发送的号码
            intent.putExtra("sms_body",sms); // 设置要发送的信息内容
            startActivity(intent);
        }


    }
}